﻿var ns = namespace("kaskelot.sp.ui");

ns.adminbyggnadtyp = function () {

	function getList() {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/GetByggnadTyper",
			type: "GET",
			data: {},
			processData: true,
			cache: false,
			success:
                function (data) {
                	if (!data.GetByggnadTyperResult.HasError) {
                		$("#byggnadtyp-results tbody").html(
							$("#byggnadtyp-template").render(data.GetByggnadTyperResult.Data)
						);
                	}
                },
			error:
                function (xhr) {
                	kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
		});
	}

	function showCreateForm() {
		var selector = "#byggnadtyp-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "ByggnadTypForm",
				idString: "",
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#byggnadtyp-form").dialog({ title: "Skapa byggnadtyp" });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "CreateByggnadTyp";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function showEditForm(id) {
		var selector = "#byggnadtyp-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "ByggnadTypForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$(selector).dialog({ title: "Redigera byggnadtyp" });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "UpdateByggnadTyp";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function showDeleteForm(id) {
		var selector = "#byggnadtyp-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "ByggnadTypForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#byggnadtyp-form").dialog({ title: "Ta bort byggnadtyp" });

				$("#byggnadtyp-form #Namn").attr("disabled", "disabled");
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "DeleteByggnadTyp";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	return {
		getList: getList,
		showEditForm: showEditForm,
		showCreateForm: showCreateForm,
		showDeleteForm: showDeleteForm
	};
}();